package com.bw.user_module.login

import androidx.lifecycle.viewModelScope
import com.bw.user_module.login.entity.LoginEntity
import com.kf.common.entity.BaseEntity
import com.kf.common.mvi.viewmodel.BaseViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.launch

class LoginViewModel : BaseViewModel<LoginModel, LoginState>() {
    override fun createState(): LoginState {
        return LoginState(BaseEntity(200, "", LoginEntity()))
    }

    override fun createModel(): LoginModel {
        return LoginModel()
    }

    fun requestLogin(map: Map<String, Any>) {
        viewModelScope.launch(Dispatchers.IO) {
            mModel.requestLogin(map)
                .flowOn(Dispatchers.IO)
                .collect {
                    mutableContainer.updateUiState { copy(entity = it) }
                }
        }
    }

}